//
// Created by Cammy on 2024/4/26.
//

#include "Dequeue.h"

void QueueInit(struct Queue *pq) {
    assert(pq);
    pq->head = pq->tail = NULL;
}

void QueueDestroy(Queue *pq) {
    assert(pq);
    QNode *cur = pq->head;
    while (cur != NULL) {
        QNode *next = cur->next;
        free(cur);
        cur = next;
    }
}

// 队尾入
void QueuePush(Queue *pq, QDataType x) {
    assert(pq);
    QNode *newnode = (QNode *) malloc(sizeof(QNode));
    if (newnode == NULL) {
        printf("malloc fail...\n");
        exit(-1);
    }

    newnode->data = x;
    newnode->next = NULL;

    if (pq->head == NULL) {
        pq->head = pq->tail = newnode;
    } else {
        pq->tail->next = newnode;
//        pq->tail=pq->tail->next;
        pq->tail = newnode;
    }
}

// 队头出
void QueuePop(Queue *pq) {
    assert(pq);
    assert(pq->head);

//    if (pq->head == pq->tail) {
    if (pq->head->next == NULL) {
        free(pq->head);
        pq->head = pq->tail = NULL;
    } else {
        QNode *headNext = pq->head->next;
        free(pq->head);
        pq->head = headNext;
    }
}

//取队头
QDataType QueueFront(Queue *pq) {
    assert(pq);
    return pq->head->data;
}

//取队尾
QDataType QueueBack(Queue *pq) {
    assert(pq);
    return pq->tail->data;
}

int QueueSize(Queue *pq) {
    assert(pq);
    int size = 0;
    QNode *cur = pq->head;
    while (cur != NULL) {
        size++;
        cur = cur->next;
    }
    return size;
}

bool QueueEmpty(Queue *pq) {
    assert(pq);
    return pq->head == NULL;
}
